package com.ten.mall.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ten.mall.mapper.PihaoMapper;
import com.ten.mall.service.PihaoService;
import com.ten.mall.vo.Pihao;
import com.ten.mall.vo.PihaoExample;
import com.ten.mall.vo.PihaoExample.Criteria;

@Service
@Transactional
public class PihaoServiceImpl implements PihaoService {
	@Autowired
	private PihaoMapper pihaoMapper;
	@Override
	@Transactional(readOnly=true)
	public Map<String, Object> selectPihao(Integer start, Integer size, String name, String goodsName) {
		PageHelper.startPage(start, size);
		PihaoExample example =new PihaoExample();
		Criteria c =example.createCriteria();
		if(StringUtils.isNoneBlank(name)){
			c.andPiNameLike("%"+name+"%");
		}
		if(StringUtils.isNoneBlank(goodsName)){
			c.andPiDetailLike("%"+goodsName+"%");
		}
		example.setOrderByClause("goods_id");
		List<Pihao> list =pihaoMapper.selectByExample(example);
		PageInfo<Pihao > info =new PageInfo<>(list);
		Map<String, Object> map =new HashMap<>();
		map.put("list", list);
		map.put("total", info.getTotal());
		return map;
	}

	@Override
	public int insertPihao(Pihao pihao) {
		int t =pihaoMapper.insert(pihao);
		return t;
	}

	@Override
	public int updatePihao(Pihao pihao) {
		int i =pihaoMapper.updateByPrimaryKeySelective(pihao);
		return i;
	}

	@Override
	public int deletePihao(Integer id) {
		int i =pihaoMapper.deleteByPrimaryKey(id);
		return i;
	}

	@Override
	@Transactional(readOnly=true)
	public Pihao selectById(Integer id) {
		Pihao pihao =pihaoMapper.selectByPrimaryKey(id);
		return pihao;
	}

	@Override
	@Transactional(readOnly=true)
	public List<Pihao> selectBygoodsId(Integer goodsId, String pihao) {
		PihaoExample example =new PihaoExample();
		Criteria c =example.createCriteria();
		if(goodsId!=null){
			c.andGoodsIdEqualTo(goodsId);
		}
		if(StringUtils.isNotBlank(pihao)){
			c.andPiNameEqualTo(pihao);
		}
		List<Pihao> list =pihaoMapper.selectByExample(example);
		return list;
	}

}
